/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Alumno;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import com.sistema.gestionuniversitaria.dataLayer.AlumnoData;
import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.CursoData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Max
 */
public class MostrarListadoMatricula extends javax.swing.JInternalFrame {

    /**
     * Creates new form MostrarListadoMatricula
     */
    CarreraData caData = new CarreraData();
    CursoData curData = new CursoData();
    AlumnoData alData = new AlumnoData();
    DefaultTableModel modelo;

    public MostrarListadoMatricula() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jComboCarreras.removeAllItems();
        cargarListaCarreras();
    }

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }
    }

    private void llenarTabla() throws SQLException, ExcepcionGlobal, NoDataException {

        Carrera careraSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
        modelo = new DefaultTableModel();
        ArrayList<Curso> listaCursos = new ArrayList<Curso>();
        Alumno alumno = alData.getAlumnoPOrCedula(jTbxCedula.getText().toUpperCase());
        listaCursos = curData.getCursosMatricula(careraSeleccionada.getCodigo(), jTbxCedula.getText().toUpperCase());
        jTbxNombre.setText(alumno.getNombre());
        jTbxApellidos.setText(alumno.getApellidos());
        jTbxCed.setText(alumno.getCedula());
        jTbxCarera.setText(careraSeleccionada.getNombre());
        modelo.setColumnCount(0);
        modelo.addColumn("Sigla");
        modelo.addColumn("Nombre");
        modelo.addColumn("Creditos");
        modelo.addColumn("Estado");
        modelo.setNumRows(listaCursos.size());
        int contador = 0;
        for (Curso curso : listaCursos) {
            modelo.setValueAt(curso.getSigla(), contador, 0);
            modelo.setValueAt(curso.getNombre(), contador, 1);
            modelo.setValueAt(curso.getCreditos(), contador, 2);
            modelo.setValueAt("Matriculado", contador, 3);
            contador = contador + 1;
        }

        jTableAlumnos.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboCarreras = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTbxCedula = new javax.swing.JTextField();
        jBtnBuscar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTbxNombre = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTbxCed = new javax.swing.JTextField();
        jTbxCarera = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTbxApellidos = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableAlumnos = new javax.swing.JTable();
        jBtnAgregar = new javax.swing.JButton();
        jBtnEliminar = new javax.swing.JButton();

        setClosable(true);
        setForeground(java.awt.Color.white);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Listado de Matricula");

        jLabel1.setText("Rellene la información correspondiente para llevar a cabo la búsqueda:");

        jLabel2.setText("Carrera:");

        jLabel3.setText("Cedula de Alumno:");

        jBtnBuscar.setText("Buscar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        jLabel4.setText("Nombre:");

        jLabel5.setText("Apellidos:");

        jTbxNombre.setEnabled(false);

        jLabel6.setText("Cédula:");

        jTbxCed.setEnabled(false);

        jTbxCarera.setEnabled(false);

        jLabel7.setText("Carrera:");

        jTbxApellidos.setEnabled(false);

        jTableAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableAlumnos);

        jBtnAgregar.setText("Agregar Curso");
        jBtnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgregarActionPerformed(evt);
            }
        });

        jBtnEliminar.setText("Eliminar Curso");
        jBtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jBtnBuscar)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTbxCedula))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(52, 52, 52)
                                .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTbxApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTbxCarera, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTbxCed, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jBtnAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBtnEliminar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(107, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTbxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addComponent(jBtnBuscar)
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTbxCed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jTbxApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTbxCarera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnEliminar)
                    .addComponent(jBtnAgregar))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 8, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEliminarActionPerformed
        int row = jTableAlumnos.getSelectedRow();
        String sigla = (String) jTableAlumnos.getValueAt(row, 0);
        try {
            curData.eliminarCursoMatricula(sigla, jTbxCed.getText());
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            llenarTabla();
            JOptionPane.showMessageDialog(null, "Éxito en la ejecución. El curso ha sido retirado para el estudiante con la cédula" + jTbxCed.getText());
        } catch (SQLException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnEliminarActionPerformed

    private void jBtnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgregarActionPerformed
        Carrera careraSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
        IngresarCursoMatricula ingresarCurso = null;
        try {
            ingresarCurso = new IngresarCursoMatricula(careraSeleccionada.getCodigo(), jTbxCed.getText(), careraSeleccionada.getNombre(),jTbxNombre.getText(), jTbxApellidos.getText());
        } catch (SQLException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        }
        FramePrincipal.jDsKtFramePrincipal.add(ingresarCurso);
        ingresarCurso.toFront();
        ingresarCurso.setVisible(true);
    }//GEN-LAST:event_jBtnAgregarActionPerformed

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        try {
            llenarTabla();
        } catch (SQLException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarListadoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAgregar;
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JButton jBtnEliminar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableAlumnos;
    private javax.swing.JTextField jTbxApellidos;
    private javax.swing.JTextField jTbxCarera;
    private javax.swing.JTextField jTbxCed;
    private javax.swing.JTextField jTbxCedula;
    private javax.swing.JTextField jTbxNombre;
    // End of variables declaration//GEN-END:variables
}
